package com.sgcc.sx.pmos.e_sx_mall.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.sgcc.sx.pmos.common.base.BasePresenter;
import com.sgcc.sx.pmos.common.bean.BaseResponse;
import com.sgcc.sx.pmos.common.bean.GoodScreenBackBean;
import com.sgcc.sx.pmos.common.http.HttpInterConst;
import com.sgcc.sx.pmos.common.http.HttpRequestUtils;
import com.sgcc.sx.pmos.common.http.MvpNetCallback;
import com.sgcc.sx.pmos.common.utils.ListPageUtil;
import com.sgcc.sx.pmos.e_sx_mall.bean.MallGoodsBean;
import com.sgcc.sx.pmos.e_sx_mall.contract.GoodsContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class GoodsPresenter extends BasePresenter<GoodsContract.View> implements GoodsContract.Presenter {

    /**
     * 商品列表
     *
     * @param pageSize
     * @param isLoading
     */
    @Override
    public void getGoodsList(int pageSize, boolean isLoading, String ratioId, String soldEnergySort, String avgPriceSort, String createDateSort, GoodScreenBackBean goodScreenBackBean) {

//        String ratioIdStr = null != goodScreenBackBean && !TextUtils.isEmpty(goodScreenBackBean.getRatioId()) ? goodScreenBackBean.getRatioId() : ratioId;
        SPUtils.getInstance().put("ratioId", ratioId);
        Map<String, Object> params = new HashMap<>();
        params.putAll(ListPageUtil.getPageInfo(pageSize));
        params.put("combName", "");
        params.put("ratioId", ratioId);
        params.put("membersSdId", "");
        params.put("sort", "");
        params.put("soldEnergySort", soldEnergySort);
        params.put("avgPriceSort", avgPriceSort);
        params.put("createDateSort", createDateSort);
        Map<String, Object> params1 = new HashMap<>();
        params.put("packageInfoCombVo", params1);
        params.put("_t", System.currentTimeMillis());
        if (goodScreenBackBean != null) {
            params.put("monthStartDate", goodScreenBackBean.getMonthStartDate());
            params.put("monthEndDate", goodScreenBackBean.getMonthEndDate());
            params.put("startDate", goodScreenBackBean.getStartDate());
            params.put("endDate", goodScreenBackBean.getEndDate());
            params.put("constrMinCyc", goodScreenBackBean.getConstrMinCyc());
            params.put("constrRel", goodScreenBackBean.getConstrRel());
            params.put("constrMod", goodScreenBackBean.getConstrMod());
            params.put("constrMin", goodScreenBackBean.getConstrMin());
            params.put("constrMax", goodScreenBackBean.getConstrMax());

            if (goodScreenBackBean.getPackageInfoCombVo() != null) {
                params.put("packageInfoCombVo", goodScreenBackBean.getPackageInfoCombVo());
                if (TextUtils.equals("2", goodScreenBackBean.getPackageInfoCombVo().getIsDoublePackage())) {
                    params.put("schemeCombVo", goodScreenBackBean.getSchemeCombVo());
                } else if (TextUtils.equals("1", goodScreenBackBean.getPackageInfoCombVo().getIsDoublePackage())) {
                    params.put("mainSchemeCombVo", goodScreenBackBean.getMainSchemeCombVo());
                    params.put("floSchemeCombVo", goodScreenBackBean.getFloSchemeCombVo());
                }
            }
        }

        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_PACKAGE, new MvpNetCallback<MallGoodsBean>(getView(), isLoading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().queryGoodsListFailed();
            }

            @Override
            public void onResponse(MallGoodsBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsListSuccess(response);
            }
        });
    }

    /**
     * 加入收藏
     *
     * @param membersUserName
     * @param membersSdId
     * @param membersSdName
     * @param combId
     * @param combName
     */
    @Override
    public void addUserPackageCollection(String membersUserName, String membersSdId, String membersSdName, String combId, String combName) {
        Map<String, Object> params = new HashMap<>();
        params.put("membersUserName", membersUserName);
        params.put("membersSdId", membersSdId);
        params.put("membersSdName", membersSdName);
        params.put("ptype", "1");
        params.put("ustatus", "01");
        params.put("combId", combId);
        params.put("combName", combName);
        params.put("remark", "");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                getView().addCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionSuccess(response, combId);
            }
        });
    }

    /**
     * 取消收藏商品/店铺
     *
     * @param combId      套餐ID
     * @param membersSdId 店铺Id
     */
    @Override
    public void deletePackageById(String combId, String membersSdId) {
        Map<String, Object> params = new HashMap<>();
        params.put("ptype", "1");
        params.put("updatePersonId", "");
        params.put("combId", combId);
        params.put("membersSdId", membersSdId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_DELE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                getView().addCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().deletePackageByIdSuccess(response, combId);
            }
        });
    }


//    @Override
//    public void onDeletePk(String guid, boolean isLoading) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("_t", System.currentTimeMillis());
//        params.put("guid", guid);
//        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_DELETE_COLLECTION, new MvpNetCallback<String>(getView(), isLoading) {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                super.onError(call, e, id);
//                // 失败回调 判断界面是否销毁
//                getView().deleteFailed();
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                super.onResponse(response, id);
//                getView().deleteSuccess();
//            }
//        });
//    }
}
